package com.xpalive.config.spring;

import com.xpalive.config.spring.exceptionhandle.GalaxyHandlerExceptionResolver;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;

import java.util.List;

/**
 * 继承WebMvcConfigurationSupport类是会导致自动配置失效的原因
 * 使用WebMvcConfigurer配置则不会覆盖掉自动配置
 */

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    /**
     * 调整MessageConverters中转换器的顺序
     *
     * @param converters
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.removeIf(StringHttpMessageConverter.class::isInstance);
        converters.add(new StringHttpMessageConverter());
    }

    /**
     * 将默认的异常处理类替换为自定义异常处理类
     *
     * @param resolvers
     */
    @Override
    public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
        resolvers.removeIf(DefaultHandlerExceptionResolver.class::isInstance);
        resolvers.add(new GalaxyHandlerExceptionResolver());
    }
//
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("swagger-ui.html")
//                .addResourceLocations("classpath:/META-INF/resources/");
//
//        registry.addResourceHandler("/webjars/**")
//                .addResourceLocations("classpath:/META-INF/resources/webjars/");
//    }

}
